classdef PointingAnalyzer
%POINTINGANALYZER Summary of this class goes here
%   Detailed explanation goes here

   properties
       FileName = '';
       Directory = '';
       ExperimentSetup;
       EventFile;
       
       %temporary variables while the EVENT logs are bad
       X;
       Y;
       Z;
       H;
       
       StartingX = 0;
       StartingY = 0;
       StartingZ = 0;
       
       FAKE = 1;
   end
   

   methods
       %function this = PointingAnalyzer(directory, file, experiment)
        %   this.Directory = directory;
        %   this.File = file;
        %   this.ExperimentSetup = experiment;
        %   this.EventFile = EventLoader(this.Directory, ...
        %                                this.FileName, ...
        %                                this.ExperimentSetup);
       %end
       
       function this = PointingAnalyzerFake(x,y,z, h)
           this.X = x;
           this.Y = y;
           this.Z = z;
           this.H = h;
       end
       
       %plots the heading. It draws a line from the last position of the
       %user in the angle used. The line has "radius" length.
       function Plot(this)
           if this.FAKE
               radius = 40;
               x = [this.X, ...
                    radius * cos(this.H-90)];
               y = [this.Y, ...
                    radius * sin(this.X-90)];
               hold on
               plot(x, y);
           else
               radius = 40;
               x = [this.EventFile.GetPointingX, ...
                    radius * cos(this.EventFile.GetPointingH-90)];
               y = [this.EventFile.GetPointingY, ...
                    radius * sin(this.EventFile.GetPointingX-90)];
               hold on
               plot(x, y);
               
           end
           
           
       end
       
       
       %figured out what the correct angle should be from the position of
       %the ball. 
       function correct = GetCorrectAngle(this)
           if this.FAKE
               correct = atan((this.Y-this.StartingY)/(this.X - this.StartingX))/ ...
                         pi * 180;
               if this.X<0
                    correct = correct-90;
               else
                    correct = correct +90;
               end  
           else
               correct = tan((this.EventFile.GetPointingY-this.StartingY) / ...
                             (this.EventFile.GetPointingX-this.StartingX) )/ ...
                         pi * -180+180;
               if this.EventFile.GetPointingX()<0
                    correct = correct-90;
               else
                    correct = correct +90;
               end
           end
       end
       
       
       % compares the heading towards the starting point, with the heading
       % that the user is lookign at. Returns an error of angles difference
       function error = GetHeadingAbsolute(this)
           correct = this.GetCorrectAngle();
           if this.FAKE
               h = this.H;
           else
               h = this.EventFile.GetPointingH; 
           end 
           
           if ( h >  90 && correct <- 90  ) || ...
              ( h <- 90 && correct >  90  )
                error = abs(180-abs(h))+ abs(180 - abs(correct));
                if correct >0
                    error = -error;
                end
           else
                error = correct -h;
           end
           error = -error;
              
       end
       
       %returns an absolute value of teh headign error.
       function error = GetHeadingError(this)
           error = abs(this.GetHeadingAbsolute());
       end
   end
end 
